package com.example.gokuplayalong.service.publicclient.service.serviceImpl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.gokuplayalong.bases.alipay.DirectPay;
import com.example.gokuplayalong.bases.base.AppParams;
import com.example.gokuplayalong.bases.base.ExceptionHandler;
import com.example.gokuplayalong.bases.base.Tools;
import com.example.gokuplayalong.bases.mysql.MysqlDruid;
import com.example.gokuplayalong.bases.push.PublishController;
import com.example.gokuplayalong.bases.security.Communication;
import com.example.gokuplayalong.service.publicclient.service.PublicClientService;
import io.micrometer.common.util.StringUtils;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Objects;

@Repository("PublicClientService")
public class PublicClientServiceImpl implements PublicClientService {
    @Override
    public String encrypt(String msg) {
        return new Communication().MessageEncrypt(msg);
    }

    @Override
    public String decrypt(String msg) {
        return new Communication().MessageDecrypt(msg);
    }

    @Override
    public String login(String msg) {
        Connection connection = null;
        PreparedStatement psmt;
        ResultSet rs;
        String userid = "";
        JSONObject msgToClient = new JSONObject();
        msgToClient.put("result", "失败");

        try {
            connection = MysqlDruid.dataSource.getConnection();
        } catch (Throwable e) {

            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());
        }
        System.out.println(msg);
        try {
            JSONObject bean = JSONObject.parseObject(msg);
            psmt = connection.prepareStatement("select count(*) from profileinfo where phone=\"" + bean.getString("phone") + "\"");
            rs = psmt.executeQuery();
            if (rs.next() && rs.getInt(1) == 0) {

                boolean result = true;
                userid = Tools.getInitRandomUserId("3", 8);
                do {
                    psmt = connection.prepareStatement("select count(*) from profileinfo where userid=\"" + userid + "\"");
                    rs = psmt.executeQuery();
                    if (rs.next() && rs.getInt(1) == 0) {
                        result = false;
                    }
                    else {
                        userid = Tools.getInitRandomUserId("3", 8);
                    }
                } while (result);

                connection.setAutoCommit(false);
                psmt = connection.prepareStatement("insert into profileinfo(registertime,nickname,userid,phone,token) values(\"" + Tools.getSDF().format(System.currentTimeMillis()) + "\",\"新用户" + Tools.getInitRandomUserId("3", 6) + "\",\"" + userid + "\",\"" + bean.getString("phone") + "\",\"" + Tools.getOrderNum() + "\")");
                psmt.executeUpdate();
                connection.commit();
            }
            else {
                connection.setAutoCommit(false);
                psmt = connection.prepareStatement("update profileinfo set deviceId=\"" + bean.getString("deviceId") + "\",ip=\"" + bean.getString("ip") + "\",token=\"" + Tools.getOrderNum() + "\" where phone=\"" + bean.getString("phone") + "\"");
                psmt.executeUpdate();
                connection.commit();

            }
            psmt = connection.prepareStatement("select * from profileinfo where phone=\"" + bean.getString("phone") + "\"");
            rs = psmt.executeQuery();
            if (rs.next()) {
                msgToClient.put("params", Tools.getResultMap(rs));
            }
            msgToClient.put("result", "成功");


        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            try {
                connection.rollback();
                if (connection != null && !connection.isClosed()) connection.close();
                return encrypt(msgToClient.toJSONString());

            } catch (Throwable ex) {
                ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), ex);
                return encrypt(msgToClient.toJSONString());
            }
        }
        try {
            if (connection != null && !connection.isClosed()) connection.close();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        System.out.println(msgToClient.toJSONString());

        return encrypt(msgToClient.toJSONString());
    }

    @Override
    public String pushRid(String msg) {
        Connection connection = null;
        PreparedStatement psmt;
        ResultSet rs;
        JSONObject msgToClient = new JSONObject();
        msgToClient.put("result", "失败");

        try {
            connection = MysqlDruid.dataSource.getConnection();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        try {
            JSONObject bean = JSONObject.parseObject(msg);

            psmt = connection.prepareStatement("select * from ridslist where phone=\"" + bean.getString("phone") + "\" or rid=\"" + bean.getString("mobRid") + "\"");
            rs = psmt.executeQuery();
            connection.setAutoCommit(false);
            if (rs.next()) {
                psmt = connection.prepareStatement("delete from ridslist where phone=\"" + bean.getString("phone") + "\" or rid=\"" + bean.getString("mobRid") + "\"");
                psmt.executeUpdate();
            }
            psmt = connection.prepareStatement("insert into ridslist(phone,rid) values (\"" + bean.getString("phone") + "\",\"" + bean.getString("mobRid") + "\")");
            psmt.executeUpdate();
            connection.commit();
            msgToClient.put("result", "成功");
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);

            try {
                connection.rollback();
                if (connection != null && !connection.isClosed()) connection.close();
                return encrypt(msgToClient.toJSONString());

            } catch (Throwable ex) {
                ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), ex);
                return encrypt(msgToClient.toJSONString());


            }
        } finally {
            try {
                connection.close();
            } catch (Throwable e) {

            }
        }
        try {
            if (connection != null && !connection.isClosed()) connection.close();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        System.out.println(msgToClient.toJSONString());
        return encrypt(msgToClient.toJSONString());


    }

    @Override
    public String getKeyParams(String msg) {
        Connection connection = null;
        PreparedStatement psmt;
        ResultSet rs;
        JSONObject msgToClient = new JSONObject();
        msgToClient.put("result", "失败");

        try {
            connection = MysqlDruid.dataSource.getConnection();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());
        }
        System.out.println(msg);
        try {
            JSONObject bean = JSONObject.parseObject(msg);


            if (bean != null) {
                connection.setAutoCommit(false);
                psmt = connection.prepareStatement("select * from profileinfo where phone=\"" + bean.getString("phone") + "\"");
                rs = psmt.executeQuery();
                if (rs.next()) {
                    msgToClient.put("params", new JSONObject(Tools.getResultMap(rs)));

                    if (rs.getInt("merchantcertify") == 1) {
                        psmt = connection.prepareStatement("select * from merchants where phone=\"" + bean.getString("phone") + "\"");
                        ResultSet rsMerchants = psmt.executeQuery();
                        if (rsMerchants.next()) {
                            msgToClient.put("merchantInfo", JSON.toJSONString(Tools.getResultMap(rsMerchants)));
                        }
                    }

                    if (!StringUtils.isEmpty(rs.getString("vipdatetime")) && System.currentTimeMillis() > Tools.getSDF().parse(rs.getString("vipdatetime")).getTime()) {
                        psmt = connection.prepareStatement("update profileinfo set viprole=0 where phone=\"" + bean.getString("phone") + "\"");
                        psmt.executeUpdate();
                    }
                    psmt = connection.prepareStatement("update profileinfo set onlinetime=\"" + Tools.getSDF().format(System.currentTimeMillis()) + "\" where phone=\"" + bean.getString("phone") + "\"");
                    psmt.executeUpdate();
                }
                connection.commit();

                msgToClient.put("result", "成功");


            }
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            try {
                if (connection != null && !connection.isClosed()) connection.close();
                return encrypt(msgToClient.toJSONString());

            } catch (Throwable ex) {
                ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), ex);
                return encrypt(msgToClient.toJSONString());


            }
        }
        try {
            if (connection != null && !connection.isClosed()) connection.close();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());
        }
        System.out.println(msgToClient.toJSONString());

        return encrypt(msgToClient.toJSONString());

    }

    @Override
    public String checkUpgrade(String msg) {
        Connection connection = null;
        PreparedStatement psmt;
        ResultSet rs;
        JSONObject msgToClient = new JSONObject();
        msgToClient.put("result", "失败");

        try {
            connection = MysqlDruid.dataSource.getConnection();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }

        try {
            psmt = connection.prepareStatement("select * from versioninfo");
            rs = psmt.executeQuery();
            if (rs.next()) {
                msgToClient.put("versionInfo", new JSONObject(Tools.getResultMap(rs)).toJSONString());
                msgToClient.put("result", "成功");
            }


        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            try {
                if (connection != null && !connection.isClosed()) connection.close();
                return encrypt(msgToClient.toJSONString());

            } catch (Throwable ex) {
                ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), ex);
                return encrypt(msgToClient.toJSONString());
            }
        } finally {
            try {
                connection.close();
            } catch (Throwable e) {

            }
        }
        try {
            if (connection != null && !connection.isClosed()) connection.close();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        System.out.println(msgToClient.toJSONString());
        return encrypt(msgToClient.toJSONString());


    }

    @Override
    public String buildOrderInfo(String msg) {
        Connection connection = null;
        PreparedStatement psmt;
        ResultSet rs;
        JSONObject msgToClient = new JSONObject();
        msgToClient.put("result", "失败");


        try {
            connection = MysqlDruid.dataSource.getConnection();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }

        try {
            JSONObject bean = JSONObject.parseObject(msg);
            psmt = connection.prepareStatement("select * from profileinfo where phone=\"" + bean.getString("phone") + "\"");
            rs = psmt.executeQuery();
            if (rs.next()) {
                if (rs.getInt("banned") == 0) {
                    bean.put("out_trade_no", Tools.getOutTradeNo());

                    connection.setAutoCommit(false);
                    psmt = connection.prepareStatement("insert into alipaylist(timestamp,phone, out_trade_no, total_amount, subject, status) values(" + System.currentTimeMillis() + ",\"" + bean.getString("phone") + "\",\"" + bean.getString("out_trade_no") + "\",\"" + bean.getDoubleValue("total_amount") + "\",\"" + bean.getString("subject") + "\",\"已创建\")");
                    psmt.executeUpdate();
                    connection.commit();

                    msgToClient.put("out_trade_no", bean.getString("out_trade_no"));
                    msgToClient.put("orderInfo", DirectPay.directPayAppPay(bean));
                    msgToClient.put("result", "成功");
                }
            }


            try {
                connection.close();
            } catch (Throwable e) {

            }
        } catch (Throwable e) {
            try {
                connection.rollback();
            } catch (Throwable ex) {
                ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), ex);
                return encrypt(msgToClient.toJSONString());
            }
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());
        } finally {
            try {
                connection.close();
            } catch (SQLException e) {
            }
        }
        try {
            if (connection != null && !connection.isClosed()) connection.close();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        System.out.println(msgToClient.toJSONString());
        return

                encrypt(msgToClient.toJSONString());

    }

    @Override
    public String alipayHandler(String msg) {
        Connection connection = null;
        PreparedStatement psmt;
        ResultSet rs;
        JSONObject msgToClient = new JSONObject();
        msgToClient.put("result", "失败");

        try {
            connection = MysqlDruid.dataSource.getConnection();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        System.out.println(msg);
        try {
            JSONObject bean = JSONObject.parseObject(msg);


            if (DirectPay.directPayAppPayQuery(bean)) {

                psmt = connection.prepareStatement("select * from alipaylist where out_trade_no=\"" + bean.get("out_trade_no") + "\"");
                rs = psmt.executeQuery();
                if (rs.next() && !Objects.equals(rs.getString("status"), "支付完成")) {

                    psmt = connection.prepareStatement("select * from profileinfo where phone=\"" + bean.getString("phone") + "\"");
                    rs = psmt.executeQuery();
                    if (rs.next()) {
                        connection.setAutoCommit(false);
                        psmt = connection.prepareStatement("insert into balancemonitor(phone,var,note,time,timestamp) values (\"" + bean.getString("phone") + "\"," + Tools.doubleFormat(bean.getDoubleValue("total_amount")) + ",\"订单支付\",\"" + Tools.getSDF().format(System.currentTimeMillis()) + "\"," + System.currentTimeMillis() + ")");
                        psmt.executeUpdate();
                        psmt = connection.prepareStatement("update alipaylist set status=\"支付完成\" where out_trade_no=\"" + bean.getString("out_trade_no") + "\"");
                        psmt.executeUpdate();
                        psmt = connection.prepareStatement("insert into sysmonitor(timestamp,msgtime, msg, msglevel) values(" + System.currentTimeMillis() + ",\"" + Tools.getSDF().format(System.currentTimeMillis()) + "\",\"支付完成-" + bean.getString("phone") + "-" + bean.get("out_trade_no") + "\",\"log\")");
                        psmt.executeUpdate();
                        psmt = connection.prepareStatement("update orderlist set outtradeno=\"" + bean.getString("out_trade_no") + "\",status=\"预约中\" where ordernum=\"" + bean.getString("ordernum") + "\"");
                        psmt.executeUpdate();
                        connection.commit();

                        msgToClient.put("result", "成功");

                    }
                }
                else {
                    msgToClient.put("msg", "已处理");

                }

            }
            else {
                connection.setAutoCommit(false);
                psmt = connection.prepareStatement("insert  into sysmonitor(timestamp,msgtime, msg, msglevel) values(" + System.currentTimeMillis() + ",\"" + Tools.getSDF().format(System.currentTimeMillis()) + "\",\"非法支付-" + bean.getString("phone") + "-" + bean.get("out_trade_no") + "\",\"danger\")");
                psmt.executeUpdate();
                psmt = connection.prepareStatement("update profileinfo set banned=1 where phone=\"" + bean.getString("phone") + "\"");
                psmt.executeUpdate();
                connection.commit();
            }

            connection.close();
        } catch (Throwable e) {
            try {
                connection.rollback();
            } catch (Throwable ex) {
                ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), ex);
                return encrypt(msgToClient.toJSONString());
            }
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());
        } finally {
            try {
                connection.close();
            } catch (Throwable e) {

            }
        }
        try {
            if (connection != null && !connection.isClosed()) connection.close();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        System.out.println(msgToClient.toJSONString());
        return encrypt(msgToClient.toJSONString());
    }

    @Override
    public String alipayCallback(HashMap<String, Object> map) {
        Connection connection = null;
        PreparedStatement psmt;
        ResultSet rs;
        String result = "failed";


        try {
            connection = MysqlDruid.dataSource.getConnection();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return result;

        }
        try {
            JSONObject body = JSON.parseObject((String) map.get("body"));

            JSONObject bean = new JSONObject();
            bean.put("out_trade_no", map.get("out_trade_no"));
            bean.put("total_amount", Double.parseDouble((String) map.get("receipt_amount")));
            if (DirectPay.directPayAppPayQuery(bean)) {
                map.put("receipt_amount", String.valueOf(bean.getDoubleValue("total_amount")));
                psmt = connection.prepareStatement("select * from alipaylist where out_trade_no=\"" + map.get("out_trade_no") + "\"");
                rs = psmt.executeQuery();
                if (rs.next() && !Objects.equals(rs.getString("status"), "支付完成")) {
                    if (Objects.equals((String) map.get("trade_status"), "TRADE_SUCCESS")) {

                        if (body != null && body.getString("phone") != null) {

                            psmt = connection.prepareStatement("select * from profileinfo where phone=\"" + body.getString("phone") + "\"");
                            rs = psmt.executeQuery();
                            if (rs.next()) {
                                connection.setAutoCommit(false);
                                double payoutsbalance = rs.getDouble("payoutsbalance");
                                double unpayoutsbalance = rs.getDouble("unpayoutsbalance") + Double.parseDouble((String) map.get("receipt_amount"));
                                psmt = connection.prepareStatement("update profileinfo set unpayoutsbalance=" + Tools.doubleFormat(unpayoutsbalance) + " where phone=\"" + body.getString("phone") + "\"");
                                psmt.executeUpdate();
                                psmt = connection.prepareStatement("insert into balancemonitor(rand,phone, var, note, time, timestamp, currentbalance) values(\"" + Tools.getOrderNum() + "\",\"" + body.getString("phone") + "\"," + Tools.doubleFormat(Double.parseDouble((String) map.get("receipt_amount"))) + ",\"余额充值" + map.get("out_trade_no") + "\",\"" + Tools.getSDF().format(System.currentTimeMillis()) + "\"," + System.currentTimeMillis() + "," + Tools.doubleFormat(payoutsbalance + unpayoutsbalance) + ")");
                                psmt.executeUpdate();
                                psmt = connection.prepareStatement("update alipaylist set status=\"支付完成\" where out_trade_no=\"" + map.get("out_trade_no") + "\"");
                                psmt.executeUpdate();
                                psmt = connection.prepareStatement("insert into sysmonitor(timestamp,msgtime, msg, msglevel) values(" + System.currentTimeMillis() + ",\"" + Tools.getSDF().format(System.currentTimeMillis()) + "\",\"充值-" + body.getString("phone") + "-" + map.get("out_trade_no") + "\",\"log\")");
                                psmt.executeUpdate();
                                connection.commit();
                                result = "success";
                            }

                        }
                        else {
                            psmt = connection.prepareStatement("insert into sysmonitor(timestamp,msgtime, msg, msglevel) values(" + System.currentTimeMillis() + ",\"" + Tools.getSDF().format(System.currentTimeMillis()) + "\",\"用户支付成功,但body为空" + map.get("out_trade_no") + "\",\"error\")");
                            psmt.executeUpdate();
                        }
                    }
                }
                else {
                    psmt = connection.prepareStatement("insert into sysmonitor(timestamp,msgtime, msg, msglevel) values(" + System.currentTimeMillis() + ",\"" + Tools.getSDF().format(System.currentTimeMillis()) + "\",\"支付回调-已处理-" + body.getString("phone") + "-" + map.get("out_trade_no") + "-" + map.get("trade_status") + "\",\"warning\")");
                    psmt.executeUpdate();
                }
            }
            else {
                connection.setAutoCommit(false);
                psmt = connection.prepareStatement("insert  into sysmonitor(timestamp,msgtime, msg, msglevel) values(" + System.currentTimeMillis() + ",\"" + Tools.getSDF().format(System.currentTimeMillis()) + "\",\"非法支付-" + body.getString("phone") + "-" + map.get("out_trade_no") + "\",\"danger\")");
                psmt.executeUpdate();
                psmt = connection.prepareStatement("update profileinfo set banned=1 where phone=\"" + body.getString("phone") + "\"");
                psmt.executeUpdate();
                connection.commit();
            }


            connection.close();
        } catch (Throwable e) {
            try {
                connection.rollback();
            } catch (Throwable ex) {
                ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), ex);
                return result;
            }
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return result;
        } finally {
            try {
                connection.close();
            } catch (Throwable e) {

            }
        }
        try {
            if (connection != null && !connection.isClosed()) connection.close();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return result;

        }
        System.out.println(result);
        return result;
    }

    @Override
    public String sendMessage(String msg) {
        Connection connection = null;
        PreparedStatement psmt;
        ResultSet rs;
        JSONObject msgToClient = new JSONObject();
        msgToClient.put("result", "失败");
        try {
            connection = MysqlDruid.dataSource.getConnection();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        try {
            JSONObject bean = JSONObject.parseObject(msg);
            connection.setAutoCommit(false);
            psmt = connection.prepareStatement("insert into matchchat(timestamp,msg,msgStatus,send,receive) values(\"" + bean.getString("timestamp") + "\",'" + bean.getString("msg") + "',\"未读\",\"" + bean.getString("phone") + "\",\"" + bean.getString("chatObject") + "\")");
            psmt.executeUpdate();
            connection.commit();
            msgToClient.put("result", "成功");

            JSONArray extras = new JSONArray();

            JSONObject item = new JSONObject();
            item.put("key", "type");
            item.put("value", "聊天消息");
            extras.add(item);
            //new PublishController().pushMessageToOne(bean.getString("chatObject"), extras.toJSONString());
//            if (Objects.equals(bean.getString("chatObject"), "13888888888") && (System.currentTimeMillis() - AppParams.getInstance().lastSendSmsTime > 24 * 60 * 1000)) {
////                Aliyun.sendSms("17310846660", "");
////                AppParams.getInstance().lastSendSmsTime = System.currentTimeMillis();
//            } else {
//                System.out.println("频率限制");
//            }


        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            try {
                connection.rollback();
                if (connection != null && !connection.isClosed()) connection.close();
                return encrypt(msgToClient.toJSONString());

            } catch (Throwable ex) {
                ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), ex);
                return encrypt(msgToClient.toJSONString());


            }
        } finally {
            try {
                connection.close();
            } catch (Throwable e) {

            }
        }
        try {
            if (connection != null && !connection.isClosed()) connection.close();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());
        }
        System.out.println(msgToClient.toJSONString());
        return encrypt(msgToClient.toJSONString());


    }

    @Override
    public String getChatPageList(String msg) {
        Connection connection = null;
        PreparedStatement psmt;
        ResultSet rs;
        JSONObject msgToClient = new JSONObject();
        msgToClient.put("result", "失败");

        try {
            connection = MysqlDruid.dataSource.getConnection();

        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        try {
            JSONObject bean = JSONObject.parseObject(msg);
            JSONArray chatList = new JSONArray();

            psmt = connection.prepareStatement("select * from (select * from (select * from matchchat where (send=\"" + bean.getString("phone") + "\" and receive=\"" + bean.getString("chatObject") + "\" ) or (send=\"" + bean.getString("chatObject") + "\" and receive=\"" + bean.getString("phone") + "\"))matchchat order by msgId desc limit " + bean.getIntValue("chatNum") * bean.getIntValue("chatCount") + ")a order by msgId");

            rs = psmt.executeQuery();
            while (rs.next()) {
                chatList.add(new JSONObject(Tools.getResultMap(rs)));

            }
            msgToClient.put("chatList", chatList.toJSONString());
            msgToClient.put("result", "成功");
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);

            try {
                if (connection != null && !connection.isClosed()) connection.close();
                return encrypt(msgToClient.toJSONString());
            } catch (Throwable ex) {
                ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), ex);
                return encrypt(msgToClient.toJSONString());

            }
        } finally {
            try {
                connection.close();
            } catch (Throwable e) {

            }
        }
        try {
            if (connection != null && !connection.isClosed()) connection.close();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        System.out.println(msgToClient.toJSONString());
        return encrypt(msgToClient.toJSONString());


    }

    @Override
    public String merchantsCreate(String msg) {
        Connection connection = null;
        PreparedStatement psmt;
        ResultSet rs;
        JSONObject msgToClient = new JSONObject();
        msgToClient.put("result", "失败");


        try {
            connection = MysqlDruid.dataSource.getConnection();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        try {
            JSONObject bean = JSONObject.parseObject(msg);

            psmt = connection.prepareStatement("select * from merchants where phone=\"" + bean.getString("phone") + "\"");
            rs = psmt.executeQuery();
            if (rs.next()) {
                connection.setAutoCommit(false);
                psmt = connection.prepareStatement("update merchants set data='" + bean.getString("data") + "',status=\"待审核\",timestamp=" + System.currentTimeMillis() + " where phone=\"" + bean.getString("phone") + "\"");
                psmt.executeUpdate();
                connection.commit();
                msgToClient.put("result", "成功");
            }
            else {
                connection.setAutoCommit(false);
                psmt = connection.prepareStatement("insert into merchants(phone,data,timestamp) values(\"" + bean.getString("phone") + "\",'" + bean.getString("data") + "'," + System.currentTimeMillis() + ")");
                psmt.executeUpdate();
                connection.commit();
                msgToClient.put("result", "成功");
            }

            try {
                connection.close();
            } catch (Throwable e) {

            }
        } catch (Throwable e) {
            try {
                connection.rollback();
            } catch (Throwable ex) {
                ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), ex);
                return encrypt(msgToClient.toJSONString());
            }
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());
        } finally {
            try {
                connection.close();
            } catch (SQLException e) {
            }
        }
        try {
            if (connection != null && !connection.isClosed()) connection.close();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        System.out.println(msgToClient.toJSONString());
        return

                encrypt(msgToClient.toJSONString());

    }

    @Override
    public String getMerchantInfo(String msg) {
        Connection connection = null;
        PreparedStatement psmt;
        ResultSet rs;
        JSONObject msgToClient = new JSONObject();
        msgToClient.put("result", "失败");


        try {
            connection = MysqlDruid.dataSource.getConnection();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        try {
            JSONObject bean = JSONObject.parseObject(msg);
            psmt = connection.prepareStatement("select * from merchants where phone=\"" + bean.getString("phone") + "\"");
            rs = psmt.executeQuery();
            if (rs.next()) {
                msgToClient.put("merchantInfo", Tools.getResultMap(rs));
            }

            psmt = connection.prepareStatement("select * from skillslist where phone=\"" + bean.getString("phone") + "\" ");
            rs = psmt.executeQuery();
            msgToClient.put("skillsList", JSON.toJSONString(Tools.convertList(rs)));

            msgToClient.put("result", "成功");


            try {
                connection.close();
            } catch (Throwable e) {

            }
        } catch (Throwable e) {
            try {
                connection.rollback();
            } catch (Throwable ex) {
                ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), ex);
                return encrypt(msgToClient.toJSONString());
            }
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());
        } finally {
            try {
                connection.close();
            } catch (SQLException e) {
            }
        }
        try {
            if (connection != null && !connection.isClosed()) connection.close();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        System.out.println(msgToClient.toJSONString());
        return

                encrypt(msgToClient.toJSONString());

    }

    @Override
    public String getUserList(String msg) {
        Connection connection = null;
        PreparedStatement psmt;
        ResultSet rs;
        JSONObject msgToClient = new JSONObject();
        msgToClient.put("result", "失败");


        try {
            connection = MysqlDruid.dataSource.getConnection();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        System.out.println(msg);

        try {
            JSONObject bean = JSONObject.parseObject(msg);


            JSONArray userList = new JSONArray();
            psmt = connection.prepareStatement("select * from profileinfo where merchantcertify=1 and phone!=\"" + bean.getString("phone") + "\" limit " + bean.getIntValue("chatNum") * bean.getIntValue("chatCount"));
            rs = psmt.executeQuery();
            while (rs.next()) {

                JSONObject user = JSON.parseObject(JSON.toJSONString(Tools.getResultMap(rs)));
                psmt = connection.prepareStatement("select * from merchants where phone=\"" + rs.getString("phone") + "\"");
                ResultSet rsMerchant = psmt.executeQuery();
                if (rsMerchant.next()) {
                    user.put("merchantInfo", JSON.toJSONString(Tools.getResultMap(rsMerchant)));
                }
                psmt = connection.prepareStatement("select * from skillslist where phone=\"" + rs.getString("phone") + "\"");
                ResultSet rsSkills = psmt.executeQuery();
                user.put("skillsList", JSON.parseArray(JSON.toJSONString(Tools.convertList(rsSkills))));


                userList.add(user);


            }
            msgToClient.put("userList", userList.toJSONString());
            msgToClient.put("result", "成功");


            try {
                connection.close();
            } catch (Throwable e) {

            }
        } catch (Throwable e) {
            try {
                connection.rollback();
            } catch (Throwable ex) {
                ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), ex);
                return encrypt(msgToClient.toJSONString());
            }
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());
        } finally {
            try {
                connection.close();
            } catch (SQLException e) {
            }
        }
        try {
            if (connection != null && !connection.isClosed()) connection.close();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        System.out.println(msgToClient.toJSONString());
        return

                encrypt(msgToClient.toJSONString());

    }

    @Override
    public String getChatList(String msg) {
        Connection connection = null;
        PreparedStatement psmt;
        ResultSet rs;
        JSONObject msgToClient = new JSONObject();
        msgToClient.put("result", "失败");

        try {
            connection = MysqlDruid.dataSource.getConnection();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        try {
            JSONObject bean = JSONObject.parseObject(msg);
            JSONArray userListTemp = new JSONArray();
            JSONArray userList = new JSONArray();
            psmt = connection.prepareStatement("select * from matchchat where send=\"" + bean.getString("phone") + "\" or receive=\"" + bean.getString("phone") + "\"");
            rs = psmt.executeQuery();
            while (rs.next()) {
                userListTemp.add(new JSONObject(Tools.getResultMap(rs)));
            }

            for (int i = 0; i < userListTemp.size(); i++) {
                if (userListTemp.getJSONObject(i).getIntValue("sameFlag") == 0) {
                    for (int j = i + 1; j < userListTemp.size(); j++) {
                        if (userListTemp.getJSONObject(j).getIntValue("sameFlag") == 0) {
                            if ((Objects.equals(userListTemp.getJSONObject(j).getString("send"), userListTemp.getJSONObject(i).getString("send")) && Objects.equals(userListTemp.getJSONObject(j).getString("receive"), userListTemp.getJSONObject(i).getString("receive"))) || (Objects.equals(userListTemp.getJSONObject(j).getString("receive"), userListTemp.getJSONObject(i).getString("send")) && Objects.equals(userListTemp.getJSONObject(j).getString("send"), userListTemp.getJSONObject(i).getString("receive")))) {
                                userListTemp.getJSONObject(j).put("sameFlag", 1);
                            }
                        }
                    }
                }
            }
            for (int i = 0; i < userListTemp.size(); i++) {
                if (userListTemp.getJSONObject(i).getIntValue("sameFlag") == 0 && (!Objects.equals(userListTemp.getJSONObject(i).getString("send"), userListTemp.getJSONObject(i).getString("receive")))) {
                    userList.add(userListTemp.getJSONObject(i));
                }
            }
            for (int i = 0; i < userList.size(); i++) {
                if (Objects.equals(userList.getJSONObject(i).getString("send"), bean.getString("phone"))) {
                    psmt = connection.prepareStatement("select * from profileinfo where phone=\"" + userList.getJSONObject(i).getString("receive") + "\"");
                }
                else {
                    psmt = connection.prepareStatement("select * from profileinfo where phone=\"" + userList.getJSONObject(i).getString("send") + "\"");
                }
                rs = psmt.executeQuery();
                if (rs.next()) {
                    userList.getJSONObject(i).putAll(new JSONObject(Tools.getResultMap(rs)));
                }
            }

            msgToClient.put("result", "成功");
            msgToClient.put("chatList", userList.toJSONString());

        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);

            try {
                if (connection != null && !connection.isClosed()) connection.close();
                return encrypt(msgToClient.toJSONString());

            } catch (Throwable ex) {
                ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), ex);
                return encrypt(msgToClient.toJSONString());
            }
        } finally {
            try {
                connection.close();
            } catch (Throwable e) {

            }
        }
        try {
            if (connection != null && !connection.isClosed()) connection.close();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        System.out.println(msgToClient.toJSONString());
        return encrypt(msgToClient.toJSONString());


    }


    @Override
    public String modifyNickName(String msg) {
        Connection connection = null;
        PreparedStatement psmt;
        ResultSet rs;
        JSONObject msgToClient = new JSONObject();
        msgToClient.put("result", "失败");

        try {
            connection = MysqlDruid.dataSource.getConnection();

        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        try {
            JSONObject bean = JSONObject.parseObject(msg);

            connection.setAutoCommit(false);
            psmt = connection.prepareStatement("update profileinfo set nickname=\"" + bean.getString("nickname") + "\" where phone=\"" + bean.getString("phone") + "\"");
            psmt.executeUpdate();

            connection.commit();
            msgToClient.put("result", "成功");
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);

            try {
                if (connection != null && !connection.isClosed()) connection.close();
                return encrypt(msgToClient.toJSONString());
            } catch (Throwable ex) {
                ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), ex);
                return encrypt(msgToClient.toJSONString());

            }
        } finally {
            try {
                connection.close();
            } catch (Throwable e) {

            }
        }
        try {
            if (connection != null && !connection.isClosed()) connection.close();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        System.out.println(msgToClient.toJSONString());
        return encrypt(msgToClient.toJSONString());


    }

    @Override
    public String modifyMerchantInfo(String msg) {
        Connection connection = null;
        PreparedStatement psmt;
        ResultSet rs;
        JSONObject msgToClient = new JSONObject();
        msgToClient.put("result", "失败");


        try {
            connection = MysqlDruid.dataSource.getConnection();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        try {
            JSONObject bean = JSONObject.parseObject(msg);

            JSONObject data = new JSONObject();

            psmt = connection.prepareStatement("select * from merchants where phone=\"" + bean.getString("phone") + "\"");
            rs = psmt.executeQuery();
            if (rs.next()) {
                data = JSON.parseObject(rs.getString("data"));


            }

            connection.setAutoCommit(false);
            if (Objects.equals(bean.getString("modifyType"), "merchantName")) {
                data.put("merchantName", bean.getString("merchantName"));
                psmt = connection.prepareStatement("update merchants set data='" + data.toJSONString() + "' where phone=\"" + bean.getString("phone") + "\"");
                psmt.executeUpdate();
            }
            else if (Objects.equals(bean.getString("modifyType"), "merchantIntroduce")) {
                data.put("merchantIntroduce", bean.getString("merchantIntroduce"));
                psmt = connection.prepareStatement("update merchants set data='" + data.toJSONString() + "' where phone=\"" + bean.getString("phone") + "\"");
                psmt.executeUpdate();
            }
            else if (Objects.equals(bean.getString("modifyType"), "merchantManifesto")) {
                data.put("merchantManifesto", bean.getString("merchantManifesto"));
                psmt = connection.prepareStatement("update merchants set data='" + data.toJSONString() + "' where phone=\"" + bean.getString("phone") + "\"");
                psmt.executeUpdate();
            }
            else if (Objects.equals(bean.getString("modifyType"), "servicetype")) {

                psmt = connection.prepareStatement("update merchants set servicetype='" + bean.getString("servicetype") + "' where phone=\"" + bean.getString("phone") + "\"");
                psmt.executeUpdate();
            }

            connection.commit();
            msgToClient.put("result", "成功");
            try {
                connection.close();
            } catch (Throwable e) {

            }
        } catch (Throwable e) {
            try {
                connection.rollback();
            } catch (Throwable ex) {
                ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), ex);
                return encrypt(msgToClient.toJSONString());
            }
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());
        } finally {
            try {
                connection.close();
            } catch (SQLException e) {
            }
        }
        try {
            if (connection != null && !connection.isClosed()) connection.close();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        System.out.println(msgToClient.toJSONString());
        return

                encrypt(msgToClient.toJSONString());

    }

    @Override
    public String modifyMerchantSkills(String msg) {
        Connection connection = null;
        PreparedStatement psmt;
        ResultSet rs;
        JSONObject msgToClient = new JSONObject();
        msgToClient.put("result", "失败");


        try {
            connection = MysqlDruid.dataSource.getConnection();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        try {
            JSONObject bean = JSONObject.parseObject(msg);

            psmt = connection.prepareStatement("select * from skillslist where gametype=\"" + bean.getString("gametype") + "\" and phone=\"" + bean.getString("phone") + "\"");
            rs = psmt.executeQuery();
            if (rs.next()) {
                connection.setAutoCommit(false);
                psmt = connection.prepareStatement("update skillslist set data='" + bean.getString("data") + "' where gametype=\"" + bean.getString("gametype") + "\" and phone=\"" + bean.getString("phone") + "\"");
                psmt.executeUpdate();
                connection.commit();
                msgToClient.put("result", "成功");
            }
            else {
                connection.setAutoCommit(false);
                psmt = connection.prepareStatement("insert into skillslist(timestamp,phone,gametype,data) values(" + System.currentTimeMillis() + ",\"" + bean.getString("phone") + "\",\"" + bean.getString("gametype") + "\",'" + bean.getString("data") + "')");
                psmt.executeUpdate();
                connection.commit();
                msgToClient.put("result", "成功");
            }


            try {
                connection.close();
            } catch (Throwable e) {

            }
        } catch (Throwable e) {
            try {
                connection.rollback();
            } catch (Throwable ex) {
                ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), ex);
                return encrypt(msgToClient.toJSONString());
            }
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());
        } finally {
            try {
                connection.close();
            } catch (SQLException e) {
            }
        }
        try {
            if (connection != null && !connection.isClosed()) connection.close();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        System.out.println(msgToClient.toJSONString());
        return

                encrypt(msgToClient.toJSONString());

    }


    @Override
    public String getSkillsList(String msg) {
        Connection connection = null;
        PreparedStatement psmt;
        ResultSet rs;
        JSONObject msgToClient = new JSONObject();
        msgToClient.put("result", "失败");


        try {
            connection = MysqlDruid.dataSource.getConnection();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        try {
            JSONObject bean = JSONObject.parseObject(msg);

            JSONArray skillsList = new JSONArray();
            psmt = connection.prepareStatement("select * from skillslist where phone=\"" + bean.getString("phone") + "\" order by timestamp asc");
            rs = psmt.executeQuery();
            skillsList = JSON.parseArray(JSON.toJSONString(Tools.convertList(rs)));
            msgToClient.put("skillsList", skillsList.toJSONString());
            msgToClient.put("result", "成功");

            try {
                connection.close();
            } catch (Throwable e) {

            }
        } catch (Throwable e) {
            try {
                connection.rollback();
            } catch (Throwable ex) {
                ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), ex);
                return encrypt(msgToClient.toJSONString());
            }
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());
        } finally {
            try {
                connection.close();
            } catch (SQLException e) {
            }
        }
        try {
            if (connection != null && !connection.isClosed()) connection.close();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        System.out.println(msgToClient.toJSONString());
        return

                encrypt(msgToClient.toJSONString());

    }

    @Override
    public String orderCreate(String msg) {
        Connection connection = null;
        PreparedStatement psmt;
        ResultSet rs;
        JSONObject msgToClient = new JSONObject();
        msgToClient.put("result", "失败");


        try {
            connection = MysqlDruid.dataSource.getConnection();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        try {
            JSONObject bean = JSONObject.parseObject(msg);
            boolean result = true;

            String ordernum = Tools.getOrderNum();
            do {
                psmt = connection.prepareStatement("select count(*) from orderlist where ordernum=\"" + ordernum + "\"");
                rs = psmt.executeQuery();
                if (rs.next() && rs.getInt(1) == 0) {
                    result = false;
                }
                else {
                    ordernum = Tools.getOrderNum();
                }
            } while (result);

            connection.setAutoCommit(false);
            psmt = connection.prepareStatement("insert into orderlist(merchantid,phone,ordernum,data,status) values(\"" + bean.getString("merchantId") + "\",\"" + bean.getString("phone") + "\",\"" + ordernum + "\",'" + bean.getString("data") + "',\"待支付\")");
            psmt.executeUpdate();
            connection.commit();
            msgToClient.put("ordernum", ordernum);
            msgToClient.put("result", "成功");
            try {
                connection.close();
            } catch (Throwable e) {

            }
        } catch (Throwable e) {
            try {
                connection.rollback();
            } catch (Throwable ex) {
                ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), ex);
                return encrypt(msgToClient.toJSONString());
            }
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());
        } finally {
            try {
                connection.close();
            } catch (SQLException e) {
            }
        }
        try {
            if (connection != null && !connection.isClosed()) connection.close();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        System.out.println(msgToClient.toJSONString());
        return

                encrypt(msgToClient.toJSONString());

    }

    @Override
    public String getOrderList(String msg) {
        Connection connection = null;
        PreparedStatement psmt;
        ResultSet rs;
        JSONObject msgToClient = new JSONObject();
        msgToClient.put("result", "失败");


        try {
            connection = MysqlDruid.dataSource.getConnection();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        System.out.println(msg);

        try {
            JSONObject bean = JSONObject.parseObject(msg);


            JSONArray orderList = new JSONArray();
            psmt = connection.prepareStatement("select * from orderlist where phone=\"" + bean.getString("phone") + "\" or merchantid=\"" + bean.getString("merchantId") + "\" order by ordernum desc limit " + bean.getIntValue("chatNum") * bean.getIntValue("chatCount"));
            rs = psmt.executeQuery();
            while (rs.next()) {
                JSONObject order = JSON.parseObject(JSON.toJSONString(Tools.getResultMap(rs)));

                psmt = connection.prepareStatement("select * from merchants where merchantid=\"" + rs.getString("merchantid") + "\"");
                ResultSet rsMerchants = psmt.executeQuery();
                if (rsMerchants.next()) {
                    order.put("merchantInfo", JSON.toJSONString(Tools.getResultMap(rsMerchants)));
                }

                orderList.add(order);
            }


            msgToClient.put("orderList", orderList.toJSONString());
            msgToClient.put("result", "成功");

            try {
                connection.close();
            } catch (Throwable e) {

            }
        } catch (Throwable e) {
            try {
                connection.rollback();
            } catch (Throwable ex) {
                ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), ex);
                return encrypt(msgToClient.toJSONString());
            }
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());
        } finally {
            try {
                connection.close();
            } catch (SQLException e) {
            }
        }
        try {
            if (connection != null && !connection.isClosed()) connection.close();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        System.out.println(msgToClient.toJSONString());
        return

                encrypt(msgToClient.toJSONString());

    }

    @Override
    public String orderStart(String msg) {
        Connection connection = null;
        PreparedStatement psmt;
        ResultSet rs;
        JSONObject msgToClient = new JSONObject();
        msgToClient.put("result", "失败");


        try {
            connection = MysqlDruid.dataSource.getConnection();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        try {
            JSONObject bean = JSONObject.parseObject(msg);

            connection.setAutoCommit(false);
            psmt = connection.prepareStatement("update orderlist set status=\"陪玩中\" where ordernum=\"" + bean.getString("ordernum") + "\"");
            psmt.executeUpdate();
            connection.commit();
            msgToClient.put("result", "成功");

            try {
                connection.close();
            } catch (Throwable e) {

            }
        } catch (Throwable e) {
            try {
                connection.rollback();
            } catch (Throwable ex) {
                ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), ex);
                return encrypt(msgToClient.toJSONString());
            }
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());
        } finally {
            try {
                connection.close();
            } catch (SQLException e) {
            }
        }
        try {
            if (connection != null && !connection.isClosed()) connection.close();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        System.out.println(msgToClient.toJSONString());
        return

                encrypt(msgToClient.toJSONString());

    }

    @Override
    public String orderFinish(String msg) {
        Connection connection = null;
        PreparedStatement psmt;
        ResultSet rs;
        JSONObject msgToClient = new JSONObject();
        msgToClient.put("result", "失败");


        try {
            connection = MysqlDruid.dataSource.getConnection();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        try {
            JSONObject bean = JSONObject.parseObject(msg);

            connection.setAutoCommit(false);
            psmt = connection.prepareStatement("update orderlist set status=\"已完成\" where ordernum=\"" + bean.getString("ordernum") + "\"");
            psmt.executeUpdate();
            connection.commit();
            msgToClient.put("result", "成功");

            try {
                connection.close();
            } catch (Throwable e) {

            }
        } catch (Throwable e) {
            try {
                connection.rollback();
            } catch (Throwable ex) {
                ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), ex);
                return encrypt(msgToClient.toJSONString());
            }
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());
        } finally {
            try {
                connection.close();
            } catch (SQLException e) {
            }
        }
        try {
            if (connection != null && !connection.isClosed()) connection.close();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        System.out.println(msgToClient.toJSONString());
        return

                encrypt(msgToClient.toJSONString());

    }

    @Override
    public String installSubmit(String msg) {
        Connection connection = null;
        PreparedStatement psmt;
        ResultSet rs;
        JSONObject msgToClient = new JSONObject();
        msgToClient.put("result", "失败");


        try {
            connection = MysqlDruid.dataSource.getConnection();

        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        try {
            JSONObject bean = JSONObject.parseObject(msg);
            connection.setAutoCommit(false);
            psmt = connection.prepareStatement("insert into installlist(phone, deviceid, time) values(\"" + bean.getString("phone") + "\",\"" + bean.getString("deviceId") + "\",\"" + Tools.getSDF().format(System.currentTimeMillis()) + "\")");
            psmt.executeUpdate();
            connection.commit();
            msgToClient.put("result", "成功");

        } catch (Throwable e) {

            e.printStackTrace();
            try {
                connection.rollback();
            } catch (Throwable ex) {

            }
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());
        } finally {
            try {
                connection.close();
            } catch (Throwable e) {

            }
        }
        try {
            if (connection != null && !connection.isClosed()) connection.close();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        System.out.println(msgToClient.toJSONString());
        return

                encrypt(msgToClient.toJSONString());
    }

    public String template(String msg) {
        Connection connection = null;
        PreparedStatement psmt;
        ResultSet rs;
        JSONObject msgToClient = new JSONObject();
        msgToClient.put("result", "失败");


        try {
            connection = MysqlDruid.dataSource.getConnection();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        try {
            JSONObject bean = JSONObject.parseObject(msg);


            try {
                connection.close();
            } catch (Throwable e) {

            }
        } catch (Throwable e) {
            try {
                connection.rollback();
            } catch (Throwable ex) {
                ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), ex);
                return encrypt(msgToClient.toJSONString());
            }
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());
        } finally {
            try {
                connection.close();
            } catch (SQLException e) {
            }
        }
        try {
            if (connection != null && !connection.isClosed()) connection.close();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
            return encrypt(msgToClient.toJSONString());

        }
        System.out.println(msgToClient.toJSONString());
        return

                encrypt(msgToClient.toJSONString());

    }
}

